/*
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2023/10/18 18:58
@File     : user.go
@Project  : GoProject
@Desc     :
*/
package handler

import (
	"GoProject/internal/pkg/request"
	"GoProject/internal/pkg/response"
	"GoProject/internal/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

type UserHandler interface {
	Create(ctx *gin.Context)
}
type userHandler struct {
	*Handler
	userService service.UserService
}

func NewUserHandler(handler *Handler, userService service.UserService) UserHandler {
	return &userHandler{
		Handler:     handler,
		userService: userService,
	}
}

// Create 用户创建
func (r *userHandler) Create(ctx *gin.Context) {
	req := new(request.UserCreate)
	//字段验证
	if err := ctx.ShouldBind(req); err != nil {
		response.Fail(ctx, http.StatusBadRequest, response.ErrBadRequest, nil)
		return
	}
	//创建用户
	if err := r.userService.Create(ctx, req); err != nil {
		response.Fail(ctx, http.StatusBadRequest, response.ErrBadRequest, nil)
		return
	}

	response.Success(ctx, nil)
}
